package lock;

import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * Created mangcao with 2019/6/4 19:02
 * Description:
 */
public class LockTest {

    private static Lock fair = new ReentrantLock();
    private static Lock nonFair = new ReentrantLock();

    private static String DEMO_THREAD = "demo";

    /***
     *
     * 测试一下锁
     * */
    public static void main(String[] args) throws InterruptedException {

        System.out.println("main Thread name:" + Thread.currentThread().getName());
        fair.lock();

        new Thread(() -> {

            try {
                boolean b = fair.tryLock(2L, TimeUnit.SECONDS);
                System.out.println(b);
            } catch (InterruptedException e) {
                return;
            }
            System.out.println("new Thread run end : " + DEMO_THREAD);

        }, DEMO_THREAD).start();

        TimeUnit.SECONDS.sleep(5);

        System.out.println("end");
//        fair.unlock();
    }


}
